/**
* @Title: indicatorsConfiguration_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author ex-xieqi001
* @date 2014年9月24日 下午2:40:42
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('IndicatorsConfigurationCtrl', ['$scope','$http','$state','$modal','BaseURL','Modal','IndicatorsConfigurationService',
	                                function($scope,$http,$state,$modal,baseUrl,Modal,IndicatorsConfigurationService){
		
		$scope.indicatorsConfigurationService = IndicatorsConfigurationService;
		
		$scope.showTable = false;
		IndicatorsConfigurationService.getIndicatorTree();
		function onTreeNodeSelected(event, node) {
			if (node.kpiId) {
				$state.go('indicators.indicatorsInfo',{
					kpiId:node.kpiId,
					regionCode:node.regionCode
				});
				IndicatorsConfigurationService.regionCode = '';
				$scope.showTable = true;
			}
			else {
				IndicatorsConfigurationService.regionCode = node.regionCode;
				$scope.showTable = false;
			}
		}
		
		//树节点监听
		$scope.$on('fwaTree.selectedChange', onTreeNodeSelected);
		
		$scope.form = {};
		
		var indicatorsData  = [],flag = false;
		//搜索框
		$scope.onSumbit = function(){
			IndicatorsConfigurationService.filter($scope.form.indicatorsName);
		}
		//给所有统筹区配置指标
		$scope.addIndicatorAll = function() {
			if (!IndicatorsConfigurationService.regionCode) {
				Modal.alert("请选中一个区域进行初始化操作!!!");
				return false;
			}
			Modal.confirm("请确认是否需要将指标配置初始化为系统推荐值！",function(){
				IndicatorsConfigurationService.addIndicatorAll();
			});
		}
		
		
	}]).controller('IndicatorsInfoCtrl', ['$scope','$http','$modal','$stateParams','BaseURL','Modal','IndicatorsInfoService','CommunityService',
	                                function(scope,$http,$modal,$stateParams,baseUrl,Modal,IndicatorsInfoService,CommunityService){
		
		scope.indicatorsInfoService = IndicatorsInfoService;
		IndicatorsInfoService.queryIndicatorsInfo($stateParams.kpiId,$stateParams.regionCode);
		IndicatorsInfoService.kpiId = $stateParams.kpiId;
		IndicatorsInfoService.regionCode = $stateParams.regionCode;
		//获取数据字典
		scope.communityService = CommunityService;
		CommunityService.initCodeListDatas();

		scope.$watch("indicatorsInfoService.indicatorsInfo",function(v){
			if(v){
				//指标类别
				if(v.typeDatas){
					angular.forEach(scope.communityService.kpiTypeDatas,function(typeData,i){
						var checked = false;
						angular.forEach(v.typeDatas,function(data){
							if(typeData.code == data.kpiType){
								checked = true;
								return;
							}
						});
						typeData.checked = checked;
					});
				}else{
					angular.forEach(scope.communityService.kpiTypeDatas,function(data,i){
						data.checked = false;
					});
				}
				
				//适用机构
				if(v.orgDatas){
					angular.forEach(scope.communityService.fitOrg,function(orgData,i){
						var checked = false;
						angular.forEach(v.orgDatas,function(data){
							if(orgData.code == data.orgType){
								checked = true;
								return;
							}
						});
						orgData.checked = checked;
					});
				}else{
					angular.forEach(scope.communityService.fitOrg,function(data,i){
						data.checked = false;
					});
				}
				//预警规则
				if(v.warnValueDatas){
					angular.forEach(v.warnValueDatas,function(warnValueData,i){
						if ('同比' == warnValueData.valueTypeName
							||'环比' == warnValueData.valueTypeName ) {
							warnValueData.warnUnit = "%";
						}
						else {
							warnValueData.warnUnit = v.unit;
						}
						warnValueData.reasonName = warnValueData.reasonName.replace('<VALUE_TYPE>',warnValueData.valueTypeName);
						warnValueData.reasonName = warnValueData.reasonName.replace('<OPERATOR>',warnValueData.operatorName);
					});
				}
			}
		});
		
		//修改阈值
		scope.updateIndicatorsInfoValue = function(dataValue) {
        	var modalInstance = $modal.open({
        	      templateUrl: 'indicatorsValue_view.html',
        	      controller: function($scope, $modalInstance){
        	    	  $scope.form = {};
        	    	  $scope.form.value = dataValue.value;
        	    	  $scope.ok = function() {
        	    		  var item = {};
        	    		  item.data = dataValue;
        	    		  item.value = $scope.form.value;
        	    		  $modalInstance.close(item);
        	    	  }
        	    	  $scope.cancel = function() {
        	    		  $modalInstance.dismiss();
        	    	  }
        	      }
        	});
        	modalInstance.result.then(function(item) {
        		dataValue.value = item.value;
        		item.data.value = item.value;
        		IndicatorsInfoService.updateIndicatorsInfoValue(item.data);
    	    });
        }
		
		//修改预警规则阈值
		scope.updateIndicatorsWarnValue = function(dataValue) {
			
        	var modalInstance = $modal.open({
        	      templateUrl: 'indicatorsWarnValue_view.html',
        	      controller: function($scope, $modalInstance){
        	    	  $scope.form = {};
        	    	  $scope.form.value = dataValue.warnValue;
        	    	  $scope.ok = function() {
        	    		  var item = {};
        	    		  item.data = dataValue;
        	    		  item.warnValue = $scope.form.value;
        	    		  $modalInstance.close(item);
        	    	  }
        	    	  $scope.cancel = function() {
        	    		  $modalInstance.dismiss();
        	    	  }
        	      }
        	});
        	modalInstance.result.then(function(item) {
        		item.data.warnValue = item.warnValue;
        		IndicatorsInfoService.updateIndicatorsWarnValue(item.data);
        		
    	    });
        }
		
		//保存指标详细信息
		scope.saveIndicatorsInfo  = function(){
			var types = [];
			angular.forEach(scope.communityService.kpiTypeDatas,function(data,i){
				if(data.checked == true){
					types.push(data.code);
				}
			});
			scope.indicatorsInfoService.indicatorsInfo.types = types.join(",");
			var orgs = [];
			angular.forEach(scope.communityService.fitOrg,function(data,i){
				if(data.checked == true){
					orgs.push(data.code);
				}
			});
			scope.indicatorsInfoService.indicatorsInfo.orgs = orgs.join(",");
			IndicatorsInfoService.updateIndicatorsInfo();
		}
		
		//重置指标详细信息
		scope.resetIndicatorsInfo = function(){
			IndicatorsInfoService.queryIndicatorsInfo($stateParams.kpiId,$stateParams.regionCode);
		}
		
	}]);
})();